package com.ylbh.modules.reply.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.pojo.reply.dto.CommunalDTO;
import com.ylbh.common.utils.IdWorker;
import com.ylbh.common.utils.OSSClientUtil;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.ShiroUtils;
import com.ylbh.modules.reply.entity.ShoppingReply;
import com.ylbh.modules.reply.entity.ShoppingReplyType;
import com.ylbh.modules.reply.mapper.ShoppingReplyMapper;
import com.ylbh.modules.reply.mapper.ShoppingReplyTypeMapper;
import com.ylbh.modules.reply.service.ShoppingReplyService;
import com.ylbh.modules.reply.service.ShoppingReplyTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor=Exception.class)
public class ShoppingReplyTypeServiceImpl  extends ServiceImpl<ShoppingReplyTypeMapper , ShoppingReplyType> implements ShoppingReplyTypeService {

    @Autowired
    private ShoppingReplyTypeMapper shoppingReplyTypeMapper;
    
    @Autowired
    private ShoppingReplyMapper shoppingReplyMapper ;

    @Override
    public PageUtils queryReplyTypeList(CommunalDTO typeDTO) {

        Page<Object> page = new Page<>(typeDTO.getPage(), typeDTO.getLimit());
        List<ShoppingReplyType> list = shoppingReplyTypeMapper.selectReplyTypeList(typeDTO, page);
        Page<ShoppingReplyType> page1 = new Page<>();
        page1.setRecords(list).setTotal(page.getTotal());
        return new PageUtils(page1);
    }

    @Override
    public void modifyReplyTypeEnableStateById(CommunalDTO communalDTO) {
        shoppingReplyTypeMapper.updateReplyTypeEnableStateById(communalDTO);
    }

    @Override
    public String uploadTypeImg(MultipartFile file) {
        OSSClientUtil ossClient = null;
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadImg2Oss(file,"route_img");//上传文件到服务器
            if("fail".equals(imgUrl)){
                return "上传失败";
            }
            return "http://image.yilianbaihui.cn/consumer/route_img/" + imgUrl;
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return "上传失败";
        }
    }

    @Override
    public void addReplyTypeClass(ShoppingReplyType shoppingReplyType) {
        IdWorker idW = new IdWorker();
        Date date = new Date();
        shoppingReplyType.setImgPath("route_img");
        shoppingReplyType.setImgName(shoppingReplyType.getImgName().substring(shoppingReplyType.getImgName().lastIndexOf("/")+1));
        shoppingReplyType.setTypeId(idW.nextId());
        shoppingReplyType.setCreateTime(date);
        shoppingReplyType.setCreateUserId(ShiroUtils.getUserId().toString());
        shoppingReplyType.setUpdateTime(date);
        shoppingReplyType.setUpdateUserId(ShiroUtils.getUserId().toString());
        shoppingReplyTypeMapper.insertReplyTypeClass(shoppingReplyType);
    }

    @Override
    public ShoppingReplyType getReplyTypeClassById(String typeId) {
        return shoppingReplyTypeMapper.selectById(typeId);
    }

    @Override
    public void removeReplyTypeClassById(String typeId) {
        if (shoppingReplyTypeMapper.deleteById(typeId) >0) {
            shoppingReplyMapper.deleteByTypeId(typeId);
        }
    }

    @Override
    public void modifyReplyTypeClassById(ShoppingReplyType shoppingReplyType) {
        shoppingReplyType.setImgPath("route_img");
        shoppingReplyType.setImgName(shoppingReplyType.getImgName().substring(shoppingReplyType.getImgName().lastIndexOf("/")+1));
        shoppingReplyType.setUpdateTime(new Date());
        shoppingReplyType.setUpdateUserId(ShiroUtils.getUserId().toString());
        shoppingReplyTypeMapper.updateReplyTypeClassById(shoppingReplyType);
    }

    @Override
    public List<ShoppingReplyType> queryTypeNameList() {
        return shoppingReplyTypeMapper.selectTypeNameList();
    }

}
